#include "kernel_operator.h"
#include "complex_custom_tiling.h"
constexpr int32_t BUFFER_NUM = 2; // tensor num for each queues

class KernelComplex {
public:
    __aicore__ inline KernelComplex () {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z,int32_t totalLength, uint32_t tileNum)
    {
        this->totalLength=totalLength;
        this->blockLength = totalLength / AscendC::GetBlockNum();
        this->tileNum = tileNum;
        this->tileLength = this->blockLength / tileNum / BUFFER_NUM;
        xGm.SetGlobalBuffer((__gm__ float *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        yGm.SetGlobalBuffer((__gm__ float *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        zGm.SetGlobalBuffer((__gm__ float *)z + 2*this->blockLength * AscendC::GetBlockIdx(), this->blockLength);

        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(float));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(float));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, 2*this->tileLength * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.AllocTensor<float>();

        AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
        AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);

        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);

    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.DeQue<float>();

        AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>();

         for (int i = 0; i < this->tileLength; i++) {
            float real = xLocal.GetValue(i);
            float imag = yLocal.GetValue(i);
            zLocal.SetValue(2 * i, real);     // 存实部
            zLocal.SetValue(2 * i + 1, imag); // 存虚部
        }

        outQueueZ.EnQue<float>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueX.FreeTensor(yLocal);

        
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {   

        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::DataCopy(zGm[progress * this->tileLength*2], zLocal, this->tileLength*2);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX, inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;

    AscendC::GlobalTensor<float> xGm;
    AscendC::GlobalTensor<float> yGm;
    AscendC::GlobalTensor<float> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    int32_t totalLength;

};

extern "C" __global__ __aicore__ void complex_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z,ComplexCustomTilingData tiling)
{
    KernelComplex op;
    op.Init(x, y, z,tiling.totalLength, tiling.tileNum);
    op.Process();
}

